package com.zdb.pojo;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.List;
import java.util.stream.Collectors;


@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginUser implements UserDetails {
    private Long id;
    private String username;
    private String password;
    private Integer enabled;
    private List<String> permissions;

    private List<GrantedAuthority> authorities;


    @Override
    public List<GrantedAuthority> getAuthorities() {
        if (ObjectUtil.isNull( permissions)){
            return null;
        }
        List<GrantedAuthority> authoritie=authorities;
        if (ObjectUtil.isNull(authorities)){
         authoritie=permissions.stream()
                    .map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
        }
        return authoritie;
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return this.password;
    }

    @Override
    @JsonIgnore
    public String getUsername() {
        return this.username;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }
    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return enabled == 0;
    }


}
